package com.feicheng.persistence.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.mapper.DictMapper;
import com.feicheng.persistence.service.IDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaoge
 * @since 2024-07-16
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Override
    public ResultInfo getDictByPage(JSONObject param) {
        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");
        String code = param.getString("code");
        QueryWrapper<JSONObject> qw = new QueryWrapper<>();
        if (StringUtils.hasLength(code)) {
            qw.like("code",code);
        }
        return ResultInfo.success(this.getBaseMapper().getDictByPage(new Page<JSONObject>(current, size), qw));

    }

    @Override
    @Transactional
    public ResultInfo updateDict(JSONObject param) {
        Dict dict = param.getObject("dict",Dict.class);

        if (null != dict) {
            updateById(dict);
        }

        return ResultInfo.success();
    }

    @Override
    @Transactional
    public ResultInfo addDict(JSONObject param) {
        Dict dict = param.getObject("dict",Dict.class);


        if (!StringUtils.hasLength(dict.getCode())) {
            dict.setCode(StringGenerator.getID("CODE"));
        }
        save(dict);

        return ResultInfo.success();
    }

    @Override
    public ResultInfo deleteDict(String code) {
        removeById(code);
        return ResultInfo.success();
    }

    @Override
    @Transactional
    public ResultInfo selectDictById(String code){
        Dict dict = this.getById(code);

        return ResultInfo.success(dict);
    }

}
